//index.js
//获取应用实例
var app = getApp()
Page({
  data: {
    addressList:[]
  },

  selectTap: function (e) {
    var id = e.currentTarget.dataset.id;
    wx.request({
      url: app.globalData.rootPath +'/m/address/default',
      method: "POST",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        token: wx.getStorageSync("token"),
        id:id,
      },
      success: (res) =>{
        if(res.data.meta.code === 200){
          this.initShippingAddress();
        }else{
          wx.showModal({
            title: '提示',
            content: "设置失败,"+res.data.meta.message,
            showCancel: false
          })
        }
      }
    })
  },

  addAddess : function () {
    wx.navigateTo({
      url:"/pages/address-add/index"
    })
  },
  
  editAddess: function (e) {
    wx.navigateTo({
      url: "/pages/address-add/index?id=" + e.currentTarget.dataset.id
    })
  },
  
  onLoad: function () {

  },
  onShow : function () {
    this.initShippingAddress();
  },
  //获取所有收货地址
  initShippingAddress: function () {
    var that = this;
    wx.request({
      url: app.globalData.rootPath + "/m/address",
      data: {
        token: wx.getStorageSync("token"),
      },
      success: function (res) {
        if (res.data.meta.code === 200) {
          that.setData({
            addressList: res.data.data
          });
        }else{
          that.setData({
            addressList: null
          });
        }
      }
    })
  }
})
